package br.com.sovi.gwt.client.pesquisa;

import java.util.List;

import br.com.sovi.gwt.client.entity.Ponto;
import br.com.sovi.gwt.client.entity.pesquisa.Opcao;
import br.com.sovi.gwt.client.entity.pesquisa.Pesquisa;
import br.com.sovi.gwt.client.pesquisa.PesquisaMenuPanel.SelectionListener;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl3D;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LocationCallback;
import com.google.gwt.maps.client.geocode.Placemark;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Icon;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;
import com.google.gwt.user.client.Timer;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.layout.FitLayout;

public class PesquisaCentralPanel extends Panel implements SelectionListener {

	private MapWidget map;

	public PesquisaCentralPanel() {
		this.setLayout(new FitLayout());

		map = new MapWidget();
		map.setSize("100%", "100%");
		map.addControl(new LargeMapControl3D());

		new Timer() {
			@Override
			public void run() {
				map.checkResizeAndCenter();
			}
		}.schedule(1);

		this.add(map);
		this.doLayout();
	}

	private void populateScreen(final Pesquisa pesquisa) {
		map.clearOverlays();

		map.setCenter(LatLng.newInstance(-23.430647, -51.932716), 13);

		List<Opcao> opcoes = pesquisa.getOpcoes();

		for (Opcao opcao : opcoes) {
			Icon icon = Icon.newInstance(opcao.getColor());
			MarkerOptions op = MarkerOptions.newInstance(icon);
			icon.setIconAnchor(com.google.gwt.maps.client.geom.Point.newInstance(10, 34));

			List<Ponto> pontos = opcao.getPoints();

			for (Ponto ponto : pontos) {
				Marker marker = new Marker(LatLng.newInstance(ponto.getLatitude(), ponto.getLongitude()), op);
				marker.addMarkerClickHandler(new PesquisaMarkerClickHandler(marker));
				map.addOverlay(marker);
			}
		}
	}

	private class PesquisaMarkerClickHandler implements MarkerClickHandler {

		private Marker marker;

		public PesquisaMarkerClickHandler(Marker marker) {
			this.marker = marker;
		}

		@Override
		public void onClick(MarkerClickEvent event) {

			new Geocoder().getLocations(marker.getLatLng(), new LocationCallback() {
				@Override
				public void onSuccess(JsArray<Placemark> locations) {
					map.setInfoWindowEnabled(true);
					InfoWindow infoWindow = map.getInfoWindow();
					InfoWindowContent content = new InfoWindowContent(locations.get(0).getAddress());
					infoWindow.open(marker.getLatLng(), content);
				}

				@Override
				public void onFailure(int statusCode) {
				}
			});

		}
	}

	@Override
	public void onClick(Pesquisa pesquisa) {
		populateScreen(pesquisa);
	}

}
